<!--
 * @FileDescription 网络连接页面
 * @Author lz
 * @Date 20230202 15:56:11
 * @LastEditors lz
 * @LastEditTime 20230202 15:56:11
-->
<template>
    <div class="wp_Net">
        <div class="NetworkBox">
            <img class="Network_tips" src="/images/Network_tips.png">
            <div class="title">提示</div>
            <close-outlined class="close" @click="close" />
            <img class="Network_line" src="/images/Network_line.png">
            <!-- <div class="txt"> {{ state.Message }}</div> -->
            <div class="txt"> {{ HomeContent.Default.Message.value }}</div>
            <div class="carNamr">
                <div class="col_l">车辆名称：</div>
                <div class="col_r_complete" style="margin-left: 18px;">
                    <a-select dropdownClassName="dropdownClass" ref="select" v-model="HomeContent.Default.deviceName.value" @change="handleChange">
                       <a-select-option v-for="item in HomeContent.Default.deviceName.value" :value="item">{{ item }}</a-select-option>
                    </a-select>
                </div> 
            </div>
            <button class="getCarName" @click="getCarName">获取车辆名称</button>
            <div class="row_Net">
                <div class="col_l">
                    服务器端口：
                </div>
                <div class="col_r_complete">
                    <a-input v-model:value="state.Port" class="complete" />
                </div>
            </div>
            <div class="row_Net_2">
                <div class="col_l">
                    服务器地址：
                </div>
                <div class="col_r_complete">
                    <a-input v-model:value="state.ServerIp" class="complete" />
                </div>
            </div>
            <div class="Connect" @click="linkBtn">
                连接
            </div>
            <div class="disConnect" @click="disconnect">
                断开连接
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ClientMessage } from '@runtime/generate/message/ClientMessage';
import WebSocketComponent from '@runtime/module/WebSocket/WebSocketComponent';
import { reactive, ref, toRefs, onBeforeMount, onMounted } from 'vue'
import { CloseOutlined } from '@ant-design/icons-vue';
import MessageCode from '../../MessageCode';
import HomeContent from '../../HomeContent';
const state = reactive({
    value: '',
    Port: 0,
    ServerIp: '',
    // Message: '服务器已断开，请重新连接服务器',
});
const CurrentSelection:any = ref()
const handleChange = (value: string) => {
  console.log(`selected ${value}`);  
  CurrentSelection.value = value
};
async function getCarName() {
    let newArr: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.SERVER_GET))
    console.log('1111111111111111111111');
    let newData: any = JSON.parse(newArr.Content).Result._v
    console.log('获取服务端可连接设备',newData[0],newData[1]);
    HomeContent.Default.deviceName.value = JSON.parse(newArr.Content).Result._v
}
async function linkBtn() {
    // state.Message = '正在连接...'
    HomeContent.Default.Message.value = '正在连接...'
    let newArr = { ServerIp: '', Port: 0 }
    newArr.ServerIp = state.ServerIp
    newArr.Port = state.Port
    //保存
    await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.CONFIG_SAVE, JSON.stringify(newArr)))
    //链接
    let res: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.SERVER_CONNECT,CurrentSelection.value))
    console.log(JSON.parse(res.Content).Message);
    // state.Message = JSON.parse(res.Content).Message
    HomeContent.Default.Message.value = JSON.parse(res.Content).Message
}
async function disconnect() {
    //断开链接
    let res: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.SERVER_DISCONNECT))
    console.log(JSON.parse(res.Content).Message);
    // state.Message = JSON.parse(res.Content).Message
    HomeContent.Default.Message.value = JSON.parse(res.Content).Message
    HomeContent.Default.deviceName.value = ''
}

onMounted(async () => {
    let newArr: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.CONFIG_GET))
    let newData: any = JSON.parse(newArr.Content).Result
    state.ServerIp = JSON.parse(newData).ServerIp
    state.Port = JSON.parse(newData).Port

    // let newArr1: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DeviceDataAdapterRequest(MessageCode.Default.MsgCodesList.SERVER_GET))
    // let newData1: any = JSON.parse(newArr1.Content).Result._v
    // HomeContent.Default.deviceName.value = JSON.parse(newArr1.Content).Result._v
    // console.log('获取服务端可连接设备',newData1[0],newData1[1]);
    
})
function close(){
    HomeContent.Default.sensorType.value = false
}

</script>

<style scoped>

.ant-select {
    width: 377px;
    height: 37px;
    margin-top: 9px;
    text-align: left;
    padding-top: 3px;
}
:deep().ant-select-selection-item {
    color: #ffffff;
}
:deep().ant-select-arrow {
    color: #ffffff !important;
}
:deep().ant-select:not(.ant-select-customize-input) .ant-select-selector {
    border: none !important;
    background: none !important;
    height: 35px;
}
:deep().ant-select-selection:focus {
    border: none;
    background: none;
}
.wp_Net {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: RGBA(0, 0, 0, 0.3);
}

.NetworkBox {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 858px;
    height: 395px;
    background-image: url('/images/Network_bg.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

.Network_tips {
    width: 55px;
    margin: 10px;
    margin-bottom: 0px;
}

.title {
    position: absolute;
    left: 62px;
    top: 24px;
    font-size: 18px;
    font-weight: bold;
    color: #FEFFFF;
}

.txt {
    font-size: 20px;
    font-weight: bold;
    color: #FFFFFF;
    padding-left: 170px;
}

.Network_line {
    padding-left: 20px;
    width: 814px;
    height: 36px;
}

.row_Net {
    overflow: hidden;
    /* padding-top: 40px; */
}

.row_Net_2 {
    overflow: hidden;
}

.col_l {
    padding-left: 170px;
    font-size: 20px;
    font-weight: 400;
    color: #FFFFFF;
    display: inline-block;
    vertical-align: middle
}

.col_r {
    width: 99px;
    height: 59px;
    background-image: url('/images/Network_i1.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    display: inline-block;
    vertical-align: middle
}

.col_r_complete {
    width: 399px;
    height: 59px;
    background-image: url('/images/Network_i2.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    display: inline-block;
    vertical-align: middle;
    text-align: center;
}
.getCarName {
    width: 100px;
    height: 57px;
    color: azure;
    border: none;
    position: absolute;
    top: 133px;
    left: 685px;
    background-image: url('/images/Network_i2.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    display: inline-block;
    vertical-align: middle;
    text-align: center;
}

.NetworkField {
    width: 79px !important;
    height: 38px !important;
    text-align: center;
}

.complete {
    width: 375px !important;
    height: 38px !important;
}

.Connect {
    width: 179px;
    height: 69px;
    background-image: url('/images/Network_btn.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    margin: 0 auto;
    text-align: center;
    line-height: 69px;
    font-size: 18px;
    font-weight: bold;
    color: #FFFFFF;
    display: inline-block;
    margin-left: 237px;
}
.disConnect {
    width: 179px;
    height: 69px;
    background-image: url('/images/Network_btn.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    margin: 0 auto;
    text-align: center;
    line-height: 69px;
    font-size: 18px;
    font-weight: bold;
    color: #FFFFFF;
    display: inline-block;
    margin-left: 30px;
}
.B1 {
    margin-left: 260px;
}

.close {
    color: #fff;
    font-size: 24px;
    position: absolute;
    right: 26px;
    top: 26px;
}

:deep().col_r_complete>.ant-input {
    margin-left: 0;
}
:global(.dropdownClass){
    background-color: #021e3d !important;
    border: 1px solid #19497d
}
:global(.ant-select-item) {
   color: #ffffff !important;
}
:global(.ant-select-item-option-active:not(.ant-select-item-option-disabled)) {
    background-color: #4d6485 !important;
}
:global(.ant-select-dropdown) {
   padding: 0;
}
:global(.ant-select-item-option-selected:not(.ant-select-item-option-disabled)) {
    color: #fff !important;
    background: #4d6485 !important;
}
</style>